package bmatch.tech.cloud.resource;


import bmatch.tech.cloud.auth.TokenNeeded;
import bmatch.tech.cloud.model.Response;
import bmatch.tech.cloud.service.FileService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import java.io.InputStream;

import org.glassfish.jersey.media.multipart.FormDataParam;

/**
 * MyFileResource
 *
 * @author: Awake
 * @date: 2018-11-29 17:16
 **/
@Component
@Path("file")
public class FileResource {
    /**
     * 日志输出
     */
    private static Logger log = LoggerFactory.getLogger(UserResource.class);

    /**
     * 文件服务类
     */
    FileService fileService;

    @Autowired
    public FileResource(FileService fileService) {
        this.fileService = fileService;
    }

    @GET
    @Path("upload")
    @Produces(MediaType.TEXT_HTML)
    @TokenNeeded
    public InputStream uploadPage() {
        ClassLoader loader = FileResource.class.getClassLoader();
        return loader.getResourceAsStream("templates/uploadForm.html");
    }

    @POST
    @Path("upload")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    @Produces(MediaType.APPLICATION_JSON)
    @TokenNeeded
    public Response upload(
            @HeaderParam("userId") String userId,
            @FormDataParam("file") InputStream inputStream,
            @FormDataParam("path") String path,
            @FormDataParam("name") String name) {

        return fileService.upload(userId, inputStream, path, name);
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @TokenNeeded
    @Path("download")
    public javax.ws.rs.core.Response downloadFile(
            @HeaderParam("userId") String userId,
            @QueryParam("path") String path,
            @QueryParam("name") String name) {

        return fileService.download(userId, path, name);
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @TokenNeeded
    @Path("delete")
    public Response delete(@HeaderParam("userId") String userId,
                           @QueryParam("path") String path,
                           @QueryParam("name") String name) {
        return fileService.delete(userId, path, name);
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @TokenNeeded
    @Path("rename")
    public Response rename(@HeaderParam("userId") String userId,
                           @QueryParam("path") String path,
                           @QueryParam("originalName") String originalName,
                           @QueryParam("targetName") String targetName) {
        return fileService.rename(userId, path, originalName, targetName);
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @TokenNeeded
    @Path("shift")
    public Response shift(@HeaderParam("userId") String userId,
                          @QueryParam("name") String name,
                          @QueryParam("originalPath") String originalPath,
                          @QueryParam("targetPath") String targetPath) {
        return fileService.shift(userId, name, originalPath, targetPath);
    }
}
